using UnityEngine;

namespace UFrame
{

    public class EditorDrawHelper
    {
        private static Material mMaterial = null;

        static EditorDrawHelper()
        {
            mMaterial = new Material(Shader.Find(@"Particles/Alpha Blended"));
            mMaterial.hideFlags = HideFlags.HideAndDontSave;
            mMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
        }

        //设置开始状态
        public static void Begin()
        {
            GL.PushMatrix(); //保存当前Matirx
            mMaterial.SetPass(0); //刷新当前材质
            GL.LoadPixelMatrix(); //设置pixelMatrix
        }

        //设置结束状态
        public static void End()
        {
            GL.PopMatrix(); //读取之前的Matrix
        }

        //绘制线段
        public static void DrawLine(Color color, Vector2 start, Vector2 end)
        {
            GL.PushMatrix(); //保存当前Matirx
            mMaterial.SetPass(0); //刷新当前材质
            GL.LoadPixelMatrix(); //设置pixelMatrix


            GL.Begin(GL.LINES);
            GL.Color(color);
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.End();
            GL.PopMatrix(); //读取之前的Matrix
        }

        //绘制矩形方框
        public static void DrawRect(Color color, Vector2 start, Vector2 end)
        {
            GL.PushMatrix(); //保存当前Matirx
            mMaterial.SetPass(0); //刷新当前材质
            GL.LoadPixelMatrix(); //设置pixelMatrix


            GL.Begin(GL.LINES);
            GL.Color(color);
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);

            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);

            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);

            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, start.y, 0);

            GL.End();
            GL.PopMatrix(); //读取之前的Matrix
        }

        //填充矩形方框
        public static void FillRect(Color color, Vector2 start, Vector2 end)
        {
            GL.PushMatrix(); //保存当前Matirx
            mMaterial.SetPass(0); //刷新当前材质
            GL.LoadPixelMatrix(); //设置pixelMatrix

            GL.Begin(GL.TRIANGLES);
            GL.Color(color);

            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);

            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, start.y, 0);
            GL.End();
            GL.PopMatrix(); //读取之前的Matrix
        }

    }


}